package org.visionlibrary.image.filters.effects;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.acquisition.ImageFactory;
import org.visionlibrary.image.model.AbstractFilter;


//Photoshop version
public class PosterizePS extends AbstractFilter {
	protected int posterizevalue = 6;

	public PosterizePS() {
		this(6);
	}

	public PosterizePS(int posterizevalue) {
		this.posterizevalue = posterizevalue;
	}

	public int getPosterizeValue() {
		return posterizevalue;
	}

	public void setPosterizeValue(int posterizevalue) {
		this.posterizevalue = posterizevalue;
	}

	@Override
	public TiledImage applyFilter(TiledImage src, TiledImage dest) {
		if (null == src)
			throw new NullPointerException("Source image is null.");

		if (null == dest)
			dest = (new ImageFactory()).createCompatibleImage(src);
		
		int maxX = src.getWidth();
		int maxY = src.getHeight();

		int numOfAreas = 256 / this.posterizevalue;
		int numOfValues = 255 / this.posterizevalue;

		for (int x = 0; x < maxX; x++)
			for (int y = 0; y < maxY; y++) {
				for (int ch = 0; ch < src.getNumBands(); ch++) {
					int c = src.getSample(x, y, ch);
					int areac = c / numOfAreas;
					int newpixel = numOfValues * areac;
					dest.setSample(x, y, ch, newpixel);
				}
			}
		
		return dest;
	}
}
